package com.pond.pond.service.impl;

import com.pond.pond.mapper.FishingTechniqueMapper;
import com.pond.pond.mapper.FishingTechniqueCommentMapper;
import com.pond.pond.service.IFishingTechniqueStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 钓技学堂统计分析Service业务层处理
 * 
 * @author pond
 * @date 2024-01-15
 */
@Service
public class FishingTechniqueStatsServiceImpl implements IFishingTechniqueStatsService 
{
    @Autowired
    private FishingTechniqueMapper fishingTechniqueMapper;
    
    @Autowired
    private FishingTechniqueCommentMapper fishingTechniqueCommentMapper;

    /**
     * 获取统计概览数据
     * 
     * @return 统计概览数据
     */
    @Override
    public Map<String, Object> getStatsOverview()
    {
        Map<String, Object> overview = new HashMap<>();
        
        // 获取文章总数
        Map<String, Object> articleStats = fishingTechniqueMapper.selectArticleStats();
        overview.put("totalArticles", articleStats.get("totalArticles"));
        overview.put("totalReads", articleStats.get("totalReads"));
        overview.put("totalLikes", articleStats.get("totalLikes"));
        overview.put("totalComments", articleStats.get("totalComments"));
        
        return overview;
    }

    /**
     * 获取文章阅读趋势
     * 
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 阅读趋势数据
     */
    @Override
    public Map<String, Object> getReadTrend(String startDate, String endDate)
    {
        Map<String, Object> result = new HashMap<>();
        
        // 如果没有指定日期范围，默认查询最近30天
        if (startDate == null || endDate == null) {
            LocalDate now = LocalDate.now();
            endDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            startDate = now.minusDays(29).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        
        List<Map<String, Object>> trendData = fishingTechniqueMapper.selectReadTrend(startDate, endDate);
        
        result.put("dates", trendData.stream().map(item -> item.get("date")).toArray());
        result.put("reads", trendData.stream().map(item -> item.get("reads")).toArray());
        result.put("articles", trendData.stream().map(item -> item.get("articles")).toArray());
        
        return result;
    }

    /**
     * 获取分类统计
     * 
     * @return 分类统计数据
     */
    @Override
    public Map<String, Object> getCategoryStats()
    {
        Map<String, Object> result = new HashMap<>();
        
        List<Map<String, Object>> categoryData = fishingTechniqueMapper.selectCategoryStats();
        
        result.put("categories", categoryData.stream().map(item -> item.get("category")).toArray());
        result.put("counts", categoryData.stream().map(item -> item.get("count")).toArray());
        result.put("data", categoryData);
        
        return result;
    }

    /**
     * 获取热门文章排行
     * 
     * @param type 排序类型：read-阅读量，like-点赞数，comment-评论数
     * @param limit 数量限制
     * @return 热门文章数据
     */
    @Override
    public Map<String, Object> getPopularArticles(String type, Integer limit)
    {
        Map<String, Object> result = new HashMap<>();
        
        List<Map<String, Object>> popularData = fishingTechniqueMapper.selectPopularArticles(type, limit);
        
        result.put("articles", popularData);
        
        return result;
    }

    /**
     * 获取用户活跃度统计
     * 
     * @param days 统计天数
     * @return 用户活跃度数据
     */
    @Override
    public Map<String, Object> getUserActivityStats(Integer days)
    {
        Map<String, Object> result = new HashMap<>();
        
        LocalDate endDate = LocalDate.now();
        LocalDate startDate = endDate.minusDays(days - 1);
        
        String startDateStr = startDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String endDateStr = endDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        
        List<Map<String, Object>> activityData = fishingTechniqueMapper.selectUserActivityStats(startDateStr, endDateStr);
        
        result.put("dates", activityData.stream().map(item -> item.get("date")).toArray());
        result.put("activeUsers", activityData.stream().map(item -> item.get("activeUsers")).toArray());
        result.put("newUsers", activityData.stream().map(item -> item.get("newUsers")).toArray());
        
        return result;
    }

    /**
     * 获取评论统计
     * 
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 评论统计数据
     */
    @Override
    public Map<String, Object> getCommentStats(String startDate, String endDate)
    {
        Map<String, Object> result = new HashMap<>();
        
        // 如果没有指定日期范围，默认查询最近30天
        if (startDate == null || endDate == null) {
            LocalDate now = LocalDate.now();
            endDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            startDate = now.minusDays(29).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        
        List<Map<String, Object>> commentData = fishingTechniqueCommentMapper.selectCommentStats(startDate, endDate);
        
        result.put("dates", commentData.stream().map(item -> item.get("date")).toArray());
        result.put("comments", commentData.stream().map(item -> item.get("comments")).toArray());
        
        return result;
    }
}